package uk.ac.sanger.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import uk.ac.sanger.DataManager;
import uk.ac.sanger.WritebackXMLParser;

/**
 * Servlet implementation class for Servlet: WritebackDeleteGroup
 *
 */
public class WritebackDeleteGroup extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	public void init(ServletConfig config)
	throws ServletException
	{
		super.init(config);
	}

	//	This is the all-important doPost() method.
	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		try {	
			HttpSession session = request.getSession(true);
			if (session!=null){

				//first check open id
				if((String)session.getAttribute("openid")==null) {
					String message	 = "you are not logged in, please log in first!";
					session.setAttribute("message", message);
					response.sendRedirect("login.jsp");
					return;
				}

				String system_uri = (String)session.getAttribute("system_uri");

				String group=(String)session.getAttribute("group");

				//save data in db
				DataManager dbM = new DataManager();
				dbM.deleteGroup(group, system_uri);

				session.setAttribute("group","");
				//send redirect to feature upload page
				response.sendRedirect("writeback.jsp?arg=NA&missing=none&uri="+system_uri);   	
			}}
		catch (Exception err){
			err.printStackTrace(); }			
	}		


	public void doGet(HttpServletRequest request,
			HttpServletResponse response)
	throws IOException, ServletException
	{
		try
		{
			HttpSession session = request.getSession(true);
			if (session!=null){
				//"group" from URL request
				String group=request.getParameter("ccc");

				//list of groups from JSP
				String groups_list=(String)session.getValue("groups");
				String text ="";

				//only one group
				if(group.equals("") & groups_list!=""){
					if (groups_list.indexOf(";")<0)
						text=groups_list;		
				}
				else if(!group.equals(""))
				{
					//parse list that has format 1;2;3;
					int i=0;
					int value=Integer.valueOf(group).intValue();
					int index=0, new_index=0;

					//first element in list
					if (value==0) {
						text=groups_list.substring(0,groups_list.indexOf(";", index+1));
					}
					else 
					{
						while (i<value)
						{
							index = groups_list.indexOf(";", index+1);
							new_index=groups_list.indexOf(";", index+1);
							if (new_index<0) {
								new_index=groups_list.length();
							}

							text=groups_list.substring(index+1,new_index);
							//System.out.println(index + " "+text);
							i++;
						} 
					}
				}

				String system_uri = (String)session.getAttribute("system_uri");

				if (!text.equals("")){
					session.setAttribute("group",text);
					response.sendRedirect("add_group.jsp?action=delete&group="+text +"&uri="+system_uri);
				}
				else
					response.sendRedirect("writeback.jsp?arg=NA&missing=none&uri="+system_uri);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new ServletException(e.getMessage());
		}
	}
}

